<template>
  <div>
    <!-- 左边 -->
    <el-container>
      <!-- 折叠板面 -->
      <el-aside :style="{ width: isCollapse ? 'auto' : '200px' }">
        <navbar :menus="menus" :collapse="isCollapse"></navbar>
      </el-aside>
      <el-container>
        <!-- 头部 -->
        <el-header>
          <i
            class="el-collapse el-icon-arrow-down"
            :class="isCollapse ? 'collapseTransform' : ''"
            @click="handleCollapse"
          ></i>
          <h2>商城后台</h2>
          <el-dropdown>
            <span class="el-dropdown-link">
              <i class="el-icon-user-solid">{{ userName }}</i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="handleLogout"
                >注销</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </el-header>
        <!-- 主体 -->
        <el-main style="box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1)">
          <router-view></router-view>
        </el-main>
        <!-- 底部 -->
        <el-footer>
          Copyright © 2021 - 2021 LoveNews All Rights Reserved. j爱商城
          版权所有
        </el-footer>
      </el-container>
    </el-container>
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <span>确定注销？</span>
      <span slot="footer" class="dialog-footer"></span>
      <br />
      <br />
      <br />
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click="handleConfirmLogout">确定</el-button>
    </el-dialog>
  </div>
</template>

<script>
import routes from "../../router/routes";
import Navbar from "./Navbar";
import { getToken} from "../../utils/auth";
import jwtDecode from "jwt-decode";
export default {
  components: {
    Navbar,
  },
  data() {
    return {
      // 解析token中的用户名
      userName: jwtDecode(getToken()).userName,
      isCollapse: false,
      dialogVisible: false,
    };
  },
  methods: {
    resolve(arr, parentPath = "") {
      let newArr = [];
      //第一步遍历数组当中的每一个元素
      arr.forEach((route) => {
        let obj = Object.assign({}, route);

        // 当上级路径存在，并且最后一位不为/，则后面添加 /
        parentPath =
          parentPath && parentPath.lastIndexOf("/") !== parentPath.length - 1
            ? parentPath + "/"
            : parentPath;

        // 总是给加上父级路径
        obj.path = parentPath + obj.path;
        // 第二步 判断有没有children属性 如有，那么需要再处理，如果没有则直接push到一个新数组中
        if (obj.children && obj.children.length > 0) {
          obj.children = this.resolve(obj.children, obj.path);
        }
        // 如果当前路由元信息有hidden信息，那么就将其子节点提升上来
        if (obj.meta && obj.meta.hidden) {
          if (obj.children && obj.children.length > 0) {
            obj.children.forEach((item) => {
              let tmp = Object.assign({}, item);
              newArr.push(tmp);
            });
          }
        } else {
          newArr.push(obj);
        }
      });

      return newArr;
    },
    handleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    handleLogout() {
      // console.log(17173);
      this.dialogVisible = true;
    },
    handleConfirmLogout() {
      this.dialogVisible = false;
      localStorage.removeItem("accessToken");
      localStorage.removeItem("refreshToken");
      this.$router.push("/login");
    },
  },
  computed: {
    menus() {
      let arr = this.resolve(routes);

      return arr;
    },
  },
};
</script>

<style scoped>
/* 头部 */
.el-header {
  border-radius: 10px;
  background-color: #ececec;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
/* 主体 */
.el-main {
  border-radius: 10px;
  min-height: calc(100vh - 120px);
}
/* 底部 */
.el-footer {
  background-color: #ececec;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  border-radius: 10px;
  font-family: 幼圆;
}
/* 折叠板面 */
.el-collapse {
  font-size: 2rem;
}
/* 收起 */
.collapseTransform {
  transform: rotate(90deg);
}
.el-dropdown-link {
  cursor: pointer;
  color: #2b2a2a;
}
.el-icon-arrow-down {
  font-size: 25px;
}
.el-icon-user-solid {
  font-size: 25px;
}
.el-table th,
.el-table td {
  text-align: center !important;
}
</style>